package com.liip.app.ftp;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
//import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

/**
 * FTPClient 工厂
 * @Auther: luolin
 * @Date: 2020-10-14 4:06 PM
 * @Description:
 */
@Slf4j
public class FTPClientFactory extends BasePooledObjectFactory<FTPClient>{

	private FTPProperties ftpProperties;
	 
	  public FTPClientFactory(FTPProperties ftpProperties) {
	    this.ftpProperties = ftpProperties;
	  }

	  /**
	     * 建立FTP连接用PooledObject封装对象放入池中
	     * @param 
	     * @return
	     */
	@Override
	public FTPClient create() throws Exception {
		FTPClient ftpClient = new FTPClient();
	    ftpClient.setControlEncoding(ftpProperties.getEncoding());
	    ftpClient.setConnectTimeout(ftpProperties.getClientTimeout());
	    
	    try {
		      ftpClient.connect(ftpProperties.getHost(), ftpProperties.getPort());
		      int reply = ftpClient.getReplyCode();
		      if (!FTPReply.isPositiveCompletion(reply)) {
		        ftpClient.disconnect();
		        log.warn("FTPServer refused connection");
		        return null;
		      }
		      boolean result = ftpClient.login(ftpProperties.getUsername(), ftpProperties.getPassword());
		      ftpClient.setFileType(ftpProperties.getTransferFileType());
		      if (!result) {
		        log.warn("ftpClient login failed... username is {}", ftpProperties.getUsername());
		      }
		    } catch (Exception e) {
		      log.error("create ftp connection failed...{}", e);
		      throw e;
		    }
		 
		    return ftpClient;
	}

	/**
     * 用PooledObject封装对象放入池中
     * @param ftpClient
     * @return
     */
    @Override
    public PooledObject<FTPClient> wrap(FTPClient ftpClient) {
        return new DefaultPooledObject<>(ftpClient);
    }
    
    /**
     * 销毁
     * @param ftpPooled
     * @throws Exception
     */
    @Override
    public void destroyObject(PooledObject<FTPClient> ftpPooled) throws Exception {
        if (null == ftpPooled) {
            return;
        }
 
        FTPClient ftpClient = ftpPooled.getObject();
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
            }
        } catch (IOException e) {
            log.error("登出FTP服务器失败");
        } finally {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                log.error("登出FTP服务器失败");
            }
        }
    }

    /**
     * 验证FTPClient对象
     * @param ftpPooled
     * @return
     */
    @Override
    public boolean validateObject(PooledObject<FTPClient> ftpPooled) {
        try {
            FTPClient ftpClient = ftpPooled.getObject();
            return ftpClient.sendNoOp();
        } catch (IOException e) {
            log.error("验证FTP服务器失败");
        }
        return false;
    }
}